const git = require('simple-git/promise');

/*
* 上传git
* */
async function uploadGit() {
    console.log('开始上传git分支')
    //先读取到仓库 
	const GIT_REPOSITORY_ROOT = 'C:/Users/admin/Desktop/manageDevice'
	const simpleGit = git(GIT_REPOSITORY_ROOT );
	let branchResult = await simpleGit.raw(['branch']);
    let branchArr = branchResult.split('\n')
    //定义分支
	var currentBranch = 'master'
	for(let i  = 0; i < branchArr.length; i++){
		let itemBranch = branchArr[i]
		if(itemBranch.indexOf('*') != -1){
			currentBranch = itemBranch.replace('*', '')
		}
	}
    currentBranch = currentBranch.trim()
    //git 命令
	let addResult = await simpleGit.raw(['add', '.']);
	console.log(addResult)
	let commitResult = await simpleGit.raw(['commit', '-m', 'hhh：更新']);
	console.log(commitResult)
	let pushResult = await simpleGit.raw(['push', '-u', 'origin', currentBranch]);
	console.log(pushResult)
	console.log('上传成功')
}

async function start() {
	const startTime = new Date().getTime()
    await uploadGit()
    var endTime = new Date().getTime()
	var allTime = Math.ceil((endTime - startTime) / 1000)
	console.log('总共耗时：'+allTime+'秒')
}
start()
